<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "yyh_patient_power".
 *
 * @property int $id
 * @property string $var_name 参数名
 * @property string $operate
 * @property string|null $instruction 说明
 */
class Patientpower extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'yyh_patient_power';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['var_name', 'operate'], 'required'],
            [['operate'], 'string'],
            [['var_name'], 'string', 'max' => 32],
            [['instruction'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'var_name' => '参数名',
            'operate' => '权限',
            'instruction' => '说明',
        ];
    }

    //得到树形结构的患者字段数据
    public static function getTreeData($where = '', $checked_ids_string = '')
    {
        $patient_array = Patientpower::find()->where($where)->orderBy('id')->asArray()->all();
        if ($checked_ids_string != '') {
            $id_array = explode(",", $checked_ids_string);
            if (null != $id_array && count($id_array) > 0) {
                foreach ($id_array as $value_id) {
                    foreach ($patient_array as $i => $d) {
                        if ((int)$d['id'] == (int)trim($value_id)) {
                            $patient_array[$i]['checked'] = true;
                        }
                    }
                }
            }
        }

        return $patient_array;
    }

    //得到患者字段对应的说明，数据
    public static function get_var_instruction()
    {
        $res = [];
        $patient_array = Patientpower::find()->where("operate = '不显示'")->orderBy('id')->all();

        foreach ($patient_array as $p) {
            $res[$p->var_name] = $p->instruction;
        }

        return $res;
    }

    public static function get_var_value($var_name)
    {
        $var = Sysconfig::find()->where("var_name = '{$var_name}'")->one();
        if (null != $var) {
            return $var->operate;
        } else {
            return null;
        }
    }
}
